#include "tools/Diary.h"

void Diary::layout(QList<QVariant> vmessage, QString enthetic, type msgtype){
    QString message;
    foreach(QVariant var, vmessage)
    {
        message += var.toString();
    }
    layout(message, enthetic, msgtype);
}


void Diary::layout(QString message, QString enthetic, type msgtype){
    std::stringstream stream;
    QString Time = QDateTime::currentDateTime().toString("yyyy:MM:dd hh:mm:ss ");
    QSettings infosetting("GlobalTool", "RGLRLS");
    int LOGLEVEL = infosetting.value("LOGLEVEL").toInt();
    QString LOGPATH = infosetting.value("LOGPATH").toString();
    std::string time = diary_white_color.arg(Time).toStdString();
    bool LOGWRITE = infosetting.value("LOGWRITE").toBool();
    bool LOGPRINT = infosetting.value("LOGPRINT").toBool();
    switch (msgtype) {
        case Info:
            if (LOGLEVEL >= 1)
            {
                std::string infohint = QString(diary_info_color.arg("[info]")).leftJustified(10, ' ').toStdString();
                std::string funchint = QString(diary_info_color.arg(enthetic)).leftJustified(25, ' ').toStdString();
                std::string massagehint = diary_info_color.arg(message).toStdString();
                stream << time << infohint << funchint << massagehint;
            }
            break;

        case Success:
            if (LOGLEVEL >= 2)
            {
                std::string successhint = QString(diary_success_color.arg("[success]")).leftJustified(10, ' ').toStdString();
                std::string funchint = QString(diary_success_color.arg(enthetic)).leftJustified(25, ' ').toStdString();
                std::string massagehint = diary_success_color.arg(message).toStdString();
                stream << time << successhint << funchint << massagehint;
            }
            break;

        case Debug:
            if (LOGLEVEL >= 3)
            {
                std::string debughint = QString(diary_debug_color.arg("[debug]")).leftJustified(10, ' ').toStdString();
                std::string funchint = QString(diary_debug_color.arg(enthetic)).leftJustified(25, ' ').toStdString();
                std::string massagehint = diary_debug_color.arg(message).toStdString();
                stream << time << debughint << funchint << massagehint;
            }
            break;

        case Warning:
            if (LOGLEVEL >= 4)
            {
                std::string warninghint = QString(diary_warning_color.arg("[warning]")).leftJustified(10, ' ').toStdString();
                std::string funchint = QString(diary_warning_color.arg(enthetic)).leftJustified(25, ' ').toStdString();
                std::string massagehint = diary_warning_color.arg(message).toStdString();
                stream << time << warninghint << funchint << massagehint;
            }
            break;

        case Error:
            if (LOGLEVEL == 5)
            {
                std::string errorhint = QString(diary_error_color.arg("[error]")).leftJustified(10, ' ').toStdString();
                std::string funchint = QString(diary_error_color.arg(enthetic)).leftJustified(25, ' ').toStdString();
                std::string massagehint = diary_error_color.arg(message).toStdString();
                stream << time << errorhint << funchint << massagehint;
            }
            break;

        default:
            break;
    }
    if (LOGWRITE) {
        std::string printChar = stream.str();
        if (!printChar.empty()){
            print(printChar.data());
        }
    }
    if (LOGPRINT && !LOGPATH.isEmpty()) {
        std::string writeChar = stream.str();
        QString message = QString(writeChar.data()).remove(QRegularExpression("\\W{2}\\d{2}m{1}"));
        write(message.toStdString().data());
    }
}

void Diary::print(const char *message){
    qDebug() << message;
}

void Diary::write(const char *message){
    QSettings infosetting("GlobalTool", "RGLRLS");
    QString LOGPATH = infosetting.value("LOGPATH").toString();
    std::ofstream filestream(LOGPATH.toStdString().data(), std::ios::app);
    filestream << message << " \n";
    filestream.close();
}

